<template>
  <div class='container-add'>
    <el-dialog
      width='25%'
      :title="showText"
      :visible="showDialog"
      @close="btnCanel"
    >
      <!-- 表单 -->
      <el-form
        :rules="rules"
        label-width="100px"
        :model="formLabelAlign"
      >
        <el-form-item
          label="所属学科"
          prop="subjectID"
        >
          <el-select
            style="width:100%"
            v-model="formLabelAlign.subjectID"
            placeholder="选择学科"
          >
            <el-option
              v-for="item in peoples"
              :key="item.value"
              :value="item.value"
              :label="item.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="目录名称"
          prop="tagName"
        >
          <el-input v-model="formLabelAlign.tagName"></el-input>
        </el-form-item>
      </el-form>
      <el-row
        style="padding"
        type="flex"
        justify="end"
      >
        <el-col :span="9">
          <el-button
            size="small"
            @click="btnCanel"
          >取消</el-button>
          <el-button
            type="primary"
            size="small"
            @click="btnOk"
          >确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
// 获取简单列表方法
import { add, detail, update } from '@/api/hmmm/tags'
import { simple } from '@/api/hmmm/subjects'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      peoplse: [],
      formLabelAlign: {
        subjectID: '',
        tagName: ''
      },
      rules: {
        subjectID: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        tagName: [{ required: true, message: '请输入标签名称', trigger: 'blur' }]
      },
      catalogueId: null
    }
  },
  computed: {
    showText () {
      return this.catalogueId ? '编辑' : '新增'
    }
  },
  created () {
    this.simple()
    this.edit()
  },
  methods: {
    // 获取学科简单列表数据
    async simple () {
      const { data } = await simple()
      console.log(data)
      this.peoples = data
    },
    // 取消 关闭表单
    btnCanel () {
      this.formLabelAlign = {
        subjectID: '',
        tagName: ''
      }
      this.$emit('update:showDialog', false) // 关闭弹层 因为父传子的时候使用了.sync修饰符
    },
    // 确定提交
    async btnOk () {
      //  有id编辑
      if (this.catalogueId) {
        await update(this.formLabelAlign)
        // console.log(this.formLabelAlign)
        this.$parent.showDialog = false
        this.$message.success('修改成功')
        this.$parent.getToble()
      } else {
        // 新增标签数据
        await add(this.formLabelAlign)
        // 通过#parent获取父组件数据
        this.$parent.getToble()
        // 通过#parent关闭弹层
        this.$parent.showDialog = false
        // 提示成功信息
        this.$message.success('添加成功')
      }
    },
    // 获取标签详情id 并渲染 页面
    async edit (id) {
      this.catalogueId = id
      const { data } = await detail({ id: this.catalogueId })
      this.formLabelAlign = data
    }
  }
}
</script>

<style scoped lang='scss'></style>
